<script>
	import Vue from 'vue'

	export default {
		onLaunch(result) {
			const vm = this;
			const appId = vm.$Storage.getStorageSync("appId");
			if (appId) {
				if (vm.$config.app_id !== appId) {
					vm.$Storage.clearStorageSync()
					setTimeout(function() {
						if (vm.$config.app_id) vm.$Storage.setStorageSync("appId", vm.$config.app_id);
					}, 500)
				}
			} else if (vm.$config.app_id) {
				vm.$Storage.setStorageSync("appId", vm.$config.app_id);
			}
			Vue.$global_util.Login.isRefreshUserInfo = true;
			console.log('App Launch', result);
			this.$auth.AppLaunch();
			this.setSessionId();
			this.setSystemCustomBar();
			// console.log(result.query, "+++++++++++++++++++46546++++++++++++++++");
			// if (result && result.query && result.query.scene) {
			//     console.log(decodeURIComponent(result.query.scene));
			//     console.log(this.$util.qs.parse(decodeURIComponent(result.query.scene), true));
			// }
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform === 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					}
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			});
		},
		onShow() {
			console.log('App Show');
			this.updateManager();
		},
		onHide() {
			this.$Storage.clearExpiredStorageSync();
		},
		onError(result) {
			console.warn(result, "+++++++++++++++++++onError++++++++++++++++");
		},
		onPageNotFound(result) {
			console.log(result, "+++++++++++++++++++onPageNotFound++++++++++++++++");
			this.$$router.replaceAll("pages/home/index")
			// 如果是 tabbar 页面，请使用 wx.switchTab
		},

		methods: {
			updateManager() {
				//#ifdef MP-WEIXIN
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(res => {
					// 请求完新版本信息的回调
					console.log(res.hasUpdate);
				});
				updateManager.onUpdateReady(res => {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});
				});
				updateManager.onUpdateFailed(res => {
					// 新的版本下载失败
				});
				//#endif
			},
			setSessionId() {
				//#ifdef H5
				const query = this.$util.qs.parse(window.location.href, true);
				if (this.$device.isMiniWechat && !query.PHPSESSID) this.$util.Login.removeLogin();
				if (query.uid) this.$util.Login.token = query.uid;
				// if (query.share_id) this.$Storage.setStorageSync('share_id', query.share_id, '10m');
				//#endif
				//#ifdef MP-WEIXIN
				const uid = uni.getStorageSync('uid');
				if (uid) this.$util.Login.token = uid;
				//#endif
			},
			async setSystemCustomBar() {
				const vm = this;
				Vue.prototype.SystemInfo = {};
				Vue.SystemInfo = {};
				Vue.prototype.HideNavigation = Vue.$device.isAlipay || Vue.$device.isWechat;
				uni.$on('setSystemInfoSync', function(SystemInfo) {
					SystemInfo.bottomSysBarHeight = /^iPhone X/.test(SystemInfo.model) ? 34 : 0;
					Vue.prototype.SystemInfo = SystemInfo;
					Vue.SystemInfo = SystemInfo;
				});
				this.$store.commit('User/getSystemInfoSync');
			},
		}
	};
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "./style/color-ui/index";
	@import "./style/scss/index";
</style>
<style>
	@import "./style/main.css";
	@import "./components/gaoyia-parse/parse.css";
</style>
